package naga.x.platform2.utils;

import java.util.List;

public class NumberUtils {

	public static final byte[] EMPTY_BYTES = new byte[0];
	public static final short[] EMPTY_SHORTS = new short[0];
	public static final int[] EMPTY_INTS = new int[0];
	
	public static boolean isInt(String src) {
		try {
			Integer.parseInt(src);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	public static boolean isLong(String src) {
		try {
			Long.parseLong(src);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
	public static boolean isNumber(Object src) {
		return src != null && (src instanceof Number || isNumber(src.toString()));
	}
	
	public static boolean isNumber(String src) {
		if (src == null || src.isEmpty()) {
			return false;
		}
		int len = src.length();
		char c = src.charAt(0);
		if (len == 1 && (c < '0' || c > '9')) {
			return false;
		}
		if (c != '+' && c != '-' && (c < '0' || c > '9')) {
			return false;
		}
		for (int i = 1; i < len; i++) {
			c = src.charAt(i);
			if (c < '0' || c > '9') {
				return false;
			}
		}
		return true;
	}
	
	public static short[] parseShorts(String s, String separator, short[] defaultVal) {
		if (StringUtils.isEmpty(s)) {
			return defaultVal;
		}
		String[] strs = StringUtils.split(s, separator);
		int len = strs.length;
		short[] vals = new short[len];
		for (int i = 0; i < len; i++) {
			vals[i] = parseShort(strs[i]);
		}
		return vals;
	}
	
	public static int[] parseInts(String s, String separator, int[] defaultVal) {
		if (StringUtils.isEmpty(s)) {
			return defaultVal;
		}
		String[] strs = StringUtils.split(s, separator);
		int len = strs.length;
		int[] vals = new int[len];
		for (int i = 0; i < len; i++) {
			vals[i] = parseInt(StringUtils.trim(strs[i]));
		}
		return vals;
	}
	
	public static boolean parseBoolean(Object src) {
		return parseBoolean(src, false);
	}
	
	public static boolean parseBoolean(Object src, boolean defaultValue) {
		if (src == null) {
			return defaultValue;
		}
		if (src instanceof Number) {
			return ((Number) src).intValue() > 0;
		}
		return "true".equalsIgnoreCase(src.toString());
	}
	
	public static byte parseByte(Object src) {
		return parseByte(src, (byte) 0);
	}
	
	public static byte parseByte(Object src, byte defaultValue) {
		if (src == null) {
			return defaultValue;
		}
		if (src instanceof Number) {
			return ((Number) src).byteValue();
		}
		return parseByte(src.toString(), defaultValue);
	}
	
	public static byte parseByte(String s) {
		return parseByte(s, (byte) 0);
	}
	
	public static byte parseByte(String s, byte defaultValue) {
		if (StringUtils.isEmpty(s)) {
			return defaultValue;
		}
		try {
			return Byte.parseByte(s);
		} catch (Exception e) {}
		return (byte) parseDouble(s, defaultValue);
	}
	
	public static short parseShort(Object src) {
		return parseShort(src, (short) 0);
	}
	
	public static short parseShort(Object src, short defaultValue) {
		if (src == null) {
			return defaultValue;
		}
		if (src instanceof Number) {
			return ((Number) src).shortValue();
		}
		return parseShort(src.toString(), defaultValue);
	}
	
	public static short parseShort(String s) {
		return parseShort(s, (short) 0);
	}
	
	public static short parseShort(String s, short defaultValue) {
		if (StringUtils.isEmpty(s)) {
			return defaultValue;
		}
		try {
			return Short.parseShort(s);
		} catch (Exception e) {}
		return (short) parseDouble(s, defaultValue);
	}
	
	public static int parseInt(Object src) {
		return parseInt(src, 0);
	}
	
	public static int parseInt(Object src, int defaultValue) {
		if (src == null) {
			return defaultValue;
		}
		if (src instanceof Number) {
			return ((Number) src).intValue();
		}
		return parseInt(src.toString(), defaultValue);
	}
	
	public static int parseInt(String s) {
		return parseInt(s, 0);
	}
	
	public static int parseInt(String s, int defaultValue) {
		if (StringUtils.isEmpty(s)) {
			return defaultValue;
		}
		try {
			return Integer.parseInt(s);
		} catch (Exception e) {
		}
		return (int) parseDouble(s, defaultValue);
	}
	
	public static long parseLong(Object src) {
		return parseLong(src, 0L);
	}
	
	public static long parseLong(Object src, long defaultValue) {
		if (src == null) {
			return defaultValue;
		}
		if (src instanceof Number) {
			return ((Number) src).longValue();
		}
		return parseLong(src.toString(), defaultValue);
	}
	
	public static long parseLong(String s) {
		return parseLong(s, 0L);
	}
	
	public static long parseLong(String s, long defaultValue) {
		if (StringUtils.isEmpty(s)) {
			return defaultValue;
		}
		try {
			return Long.parseLong(s);
		} catch (Exception e) {}
		return (long) parseDouble(s, defaultValue);
	}
	
	public static float parseFloat(Object src) {
		return parseFloat(src, 0f);
	}
	
	public static float parseFloat(Object src, float defaultValue) {
		if (src == null) {
			return defaultValue;
		}
		if (src instanceof Number) {
			return ((Number) src).floatValue();
		}
		return parseFloat(src.toString(), defaultValue);
	}
	
	public static float parseFloat(String s) {
		return parseFloat(s, 0f);
	}
	
	public static float parseFloat(String s, float defaultValue) {
		if (StringUtils.isEmpty(s)) {
			return defaultValue;
		}
		try {
			return Float.parseFloat(s);
		} catch (Exception e) {}
		return (float) parseDouble(s, defaultValue);
	}
	
	public static double parseDouble(Object src) {
		return parseDouble(src, 0d);
	}
	
	public static double parseDouble(Object src, double defaultValue) {
		if (src == null) {
			return defaultValue;
		}
		if (src instanceof Number) {
			return ((Number) src).doubleValue();
		}
		return parseDouble(src.toString(), defaultValue);
	}
	
	public static double parseDouble(String s) {
		return parseDouble(s, 0d);
	}
	
	public static double parseDouble(String s, double doubleValue) {
		if (StringUtils.isEmpty(s)) {
			return doubleValue;
		}
		try {
			return Double.parseDouble(s);
		} catch (Exception e) {}
		return doubleValue;
	}
	
	public static char parseChar(Object src) {
		return parseChar(src, (char) 0);
	}
	
	public static char parseChar(Object src, char defaultValue) {
		if (src == null) {
			return defaultValue;
		}
		if (src instanceof Character) {
			return ((Character) src).charValue();
		}
		String s = src.toString();
		return s.length() > 0 ? s.charAt(0) : defaultValue;
	}
	
	public static boolean isEmpty(int[] vals) {
		return vals == null || vals.length == 0;
	}
	
	public static byte[] shuffle(byte[] nums) {
		for (int i = nums.length - 1; i >= 0; i--) {
			int j = CommonUtils.randomInt(i + 1);
			byte n = nums[i];
			nums[i] = nums[j];
			nums[j] = n;
		}
		return nums;
	}
	
	public static int[] shuffle(int[] nums) {
		for (int i = nums.length - 1; i >= 0; i--) {
			int j = CommonUtils.randomInt(i + 1);
			int n = nums[i];
			nums[i] = nums[j];
			nums[j] = n;
		}
		return nums;
	}
	
	public static int[] bytes2ints(byte[] bytes) {
		int len = bytes.length;
		int[] ints = new int[len];
		for (int i = len - 1; i >= 0; i--) {
			ints[i] = bytes[i] & 0xff;
		}
		return ints;
	}
	
	public static byte[] ints2bytes(int[] ints) {
		int len = ints.length;
		byte[] bytes = new byte[len];
		for (int i = len - 1; i >= 0; i--) {
			bytes[i] = (byte) (ints[i] & 0xff);
		}
		return bytes;
	}
	
	public static Integer[] ints2integers(int[] ints) {
		int len = ints.length;
		Integer[] vals = new Integer[len];
		for (int i = 0; i < len; i++) {
			vals[i] = ints[i];
		}
		return vals;
	}
	
	public static Float[] floats2objects(float[] vals) {
		int len = vals.length;
		Float[] ret = new Float[len];
		for (int i = 0; i < len; i++) {
			ret[i] = vals[i];
		}
		return ret;
	}

	public static long[] list2longs(List<? extends Number> list) {
		if (list == null || list.isEmpty()) {
			return null;
		}
		long[] l = new long[list.size()];
		for (int i = 0; i < list.size(); i++) {
			l[i] = parseLong(list.get(i));
		}
		return l;
	}
	
}
